package com.competition.bonusrule.controller;

import java.util.List;
import java.util.Set;
import javax.security.auth.Subject;
import javax.servlet.http.HttpServletResponse;

import com.competition.bonusrule.domain.vo.BonusruleVo;
import com.competition.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.competition.common.annotation.Log;
import com.competition.common.core.controller.BaseController;
import com.competition.common.core.domain.AjaxResult;
import com.competition.common.enums.BusinessType;
import com.competition.bonusrule.domain.Bonusrule;
import com.competition.bonusrule.service.IBonusruleService;
import com.competition.common.utils.poi.ExcelUtil;
import com.competition.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 奖金规则Controller
 *
 * @author cui
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/rules/bonusrule")
public class BonusruleController extends BaseController {
    @Autowired
    private IBonusruleService bonusruleService;

    /**
     * 查询奖金规则列表
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:list')")
    @GetMapping("/list")
    public TableDataInfo list(BonusruleVo bonusrule) {
        startPage();
        List<BonusruleVo> list = bonusruleService.selectBonusruleVoList(bonusrule);
        return getDataTable(list);
    }

    /**
     * 导入奖金规则文件模板
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BonusruleVo> util = new ExcelUtil<BonusruleVo>(BonusruleVo.class);
        List<BonusruleVo> list = util.importExcel(file.getInputStream());
        System.out.println(list);
        String operName = getUsername();
        String message = bonusruleService.importRules(list, updateSupport, operName);
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<BonusruleVo> util = new ExcelUtil<BonusruleVo>(BonusruleVo.class);
        util.importTemplateExcel(response, "奖金规则数据");
    }

    /**
     * 导出奖金规则列表
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:export')")
    @Log(title = "奖金规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bonusrule bonusrule) {
        List<Bonusrule> list = bonusruleService.selectBonusruleList(bonusrule);
        ExcelUtil<Bonusrule> util = new ExcelUtil<Bonusrule>(Bonusrule.class);
        util.exportExcel(response, list, "奖金规则数据");
    }

    /**
     * 获取奖金规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:query')")
    @GetMapping(value = "/{ruleId}")
    public AjaxResult getInfo(@PathVariable("ruleId") Long ruleId) {
        return success(bonusruleService.selectBonusruleByRuleId(ruleId));
    }

    /**
     * 新增奖金规则
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:add')")
    @Log(title = "奖金规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bonusrule bonusrule) {
        return toAjax(bonusruleService.insertBonusrule(bonusrule));
    }

    /**
     * 修改奖金规则
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:edit')")
    @Log(title = "奖金规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bonusrule bonusrule) {
        return toAjax(bonusruleService.updateBonusrule(bonusrule));
    }

    /**
     * 删除奖金规则
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:remove')")
    @Log(title = "奖金规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public AjaxResult remove(@PathVariable Long[] ruleIds) {
        return toAjax(bonusruleService.deleteBonusruleByRuleIds(ruleIds));
    }

    // ================ 奖金分配相关接口 ================

    /**
     * 查询奖金分配列表
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:distribution:list')")
    @GetMapping("/distribution/list")
    public TableDataInfo distributionList(BonusruleVo bonusruleVo) {
        startPage();
        List<BonusruleVo> list = bonusruleService.selectBonusDistributionList(bonusruleVo);
        return getDataTable(list);
    }

    /**
     * 匹配奖金规则
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:distribution:query')")
    @GetMapping("/distribution/match/{awardId}")
    public AjaxResult matchBonusRule(@PathVariable("awardId") Long awardId) {
        BonusruleVo result = bonusruleService.matchBonusRule(awardId);
        return success(result);
    }

    /**
     * 执行奖金分配
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:distribution:operate')")
    @Log(title = "奖金分配", businessType = BusinessType.UPDATE)
    @PostMapping("/distribution")
    public AjaxResult distribute(@RequestBody BonusruleVo bonusruleVo) {
        // 检查分配权限
        Long userId = getUserId();
        if (!bonusruleService.checkDistributionPermission(bonusruleVo.getAwardId(), userId)) {
            return error("只有第一指导老师可以分配奖金");
        }

        return toAjax(bonusruleService.distributeBonus(bonusruleVo));
    }

    /**
     * 检查分配权限
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:distribution:query')")
    @GetMapping("/distribution/checkPermission/{awardId}")
    public AjaxResult checkPermission(@PathVariable("awardId") Long awardId) {
        Long userId = getUserId();
        boolean hasPermission = bonusruleService.checkDistributionPermission(awardId, userId);
        return success(hasPermission);
    }

    /**
     * 导出奖金分配列表
     */
    @PreAuthorize("@ss.hasPermi('rules:bonusrule:distribution:list')")
    @Log(title = "奖金分配", businessType = BusinessType.EXPORT)
    @PostMapping("/distribution/export")
    public void exportDistribution(HttpServletResponse response, BonusruleVo bonusruleVo) {
        List<BonusruleVo> list = bonusruleService.selectBonusDistributionList(bonusruleVo);
        ExcelUtil<BonusruleVo> util = new ExcelUtil<BonusruleVo>(BonusruleVo.class);
        util.exportExcel(response, list, "奖金分配数据");
    }

}